package com.atguigu.gmall.controller;

import com.atguigu.gmall.bean.InteractionVideo;
import com.atguigu.gmall.server.InteractionVWService;
import com.atguigu.gmall.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class InteractionVWController {
    @Autowired
    private InteractionVWService interactionVWService;

    @GetMapping("/video")
    public String getVW(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date==0){
            date= DateFormatUtil.now();
        }

        List<InteractionVideo> interactionVideoList = interactionVWService.VWSList(date);

        StringBuilder jsonB = new StringBuilder("{\n" +
                "  \"status\": 0,\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"课程章节名称\",\n" +
                "        \"id\": \"chapter_name\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"播放次数\",\n" +
                "        \"id\": \"play_count\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"累积播放时长\",\n" +
                "        \"id\": \"play_time\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"累积观看人数\",\n" +
                "        \"id\": \"user_count\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"人均观看时长\",\n" +
                "        \"id\": \"avg_play_time\",\n" +
                "        \"accuracy\": 2\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": [");

        for (int i = 0; i < interactionVideoList.size(); i++) {
            InteractionVideo bean = interactionVideoList.get(i);
            jsonB.append("{\n" +
                    "        \"chapter_name\": \""+bean.getChapter_name()+"\",\n" +
                    "        \"play_count\": "+bean.getPlay_count()+",\n" +
                    "        \"play_time\": "+bean.getPlay_time()+",\n" +
                    "        \"user_count\": "+bean.getUser_count()+",\n" +
                    "        \"avg_play_time\": "+bean.getAvg_play_time()+"\n" +
                    "      }");
            if (i<interactionVideoList.size()-1){
                jsonB.append(",");
            }
        }
        jsonB.append("    ]\n" +
                "  }\n" +
                "}");


        return jsonB.toString();
    }
}
